/*
 * UserService.cpp
 *
 *  Created on: 2018年3月31日
 *      Author: yaoqiaobing
 */

#include "service/UserService.hpp"
#include "dao/UserDao.hpp"

UserService::UserService() {
	this->db_mysql_handle_ = new acl::db_mysql("116.196.78.242:3306", "db_merchant", "root", "123456");
	this->userDao_ = new UserDao(this->db_mysql_handle_, "/cygdrive/e/c_dev/sts_project/Hello/src/dao/mapping/UserDao.xml");
}

UserService::~UserService() {
	delete userDao_;
	userDao_ = NULL;

	delete db_mysql_handle_;
	db_mysql_handle_ = NULL;
}

int UserService::checkUserStatus() {
	// 查询参数设置
	acl::query query;

	printf("\r\n-------------------------INSERT--------------------------\r\n");
	query.reset();
	query.set_parameter("account", "137757").set_parameter("password", "2312").set_parameter("sex", 2);
	// 调用插入
	userDao_->insertUserData(query);

	printf("\r\n--------------------------SELECT-------------------------\r\n");
	query.reset();
	query.set_parameter("account", "137757");
    // 调用查询
	const acl::db_rows* result = userDao_->selectUserData(query);

	printf("\r\n---------------------------------------------------\r\n");

	int id = (*result)[0]->field_int("id", 0);

	// 列出查询结果方法一
	if (result)
	{
		const std::vector<acl::db_row*>& rows = result->get_rows();
		for (size_t i = 0; i < rows.size(); i++)
		{
			const acl::db_row* row = rows[i];
			for (size_t j = 0; j < row->length(); j++)
				printf("%s, ", (*row)[j]);
			printf("\r\n");
		}
	}

	printf("\r\n---------------------------------------------------\r\n");

	// 列出查询结果方法二
	for (size_t i = 0; i < db_mysql_handle_->length(); i++)
	{
		const acl::db_row* row = (*db_mysql_handle_)[i];
		for (size_t j = 0; j < row->length(); j++)
			printf("%s, ", (*row)[j]);
		printf("\r\n");
	}

	printf("\r\n---------------------------------------------------\r\n");

	// 列出查询结果方法三
	const std::vector<acl::db_row*>* rows = db_mysql_handle_->get_rows();
	if (rows)
	{
		std::vector<acl::db_row*>::const_iterator cit = rows->begin();
		for (; cit != rows->end(); cit++)
		{
			const acl::db_row* row = *cit;
			for (size_t j = 0; j < row->length(); j++)
				printf("%s, ", (*row)[j]);
			printf("\r\n");
		}

	}

//	// 释放查询结果
//	db_mysql_handle_->free_result();

	printf("\r\n-------------------------UPDATE--------------------------\r\n");
	query.reset();
	query.set_parameter("name", "lililili");
	// 调用更新
	userDao_->updateUserData(query);

	printf("\r\n-------------------------DELETE--------------------------\r\n");
	query.reset();
	query.set_parameter("id", id);
	// 调用更新
	userDao_->deleteUserData(query);

	return 0;
}


